<?php
/* Copyright (c) 2008 Hailey Bannister
All rights reserved.
 
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 
    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the aframephp project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 */
$time = sprintf("%.5f", microtime(true));
global $cache,$db,$afdebug;
/*
 * edit.php - Hailey Bannister
 
 * This file is for editing the bcms module nodes. Right now its kinda ghetto, but it should get bettter.
 */
      require_once('inc/conf.inc.php');

$txt = new txtparser();
$bcms = new bcms();

if(isset($_POST['sourceData'])){
$txt->loadText($_POST['sourceData']);
$txt->setParse('xhtml',"memproxy.php?req=%s","edit.php?req=%s",'*');
$txt->setPageCallback('pageExists');

if($_POST['action'] == 'Grab') {
	$res = $bcms->getNodebyKey($_POST['key']);
	$txt->loadText($res['raw']);
}


$output = $txt->renderData();
doHtml();

if($_POST['action'] == 'Commit') {

		$nodeData['key']  = $_POST['key'];
    $nodeData['raw']  = $_POST['sourceData'];
    $nodeData['html'] = $output;
    $nodeData['vis']  = $_POST['visible'];
    $nodeData['authid'] = '5';
    $nodeData['seclev'] = '5';
    
    //Now.. for something completely different..
    if($_POST['visible'] == 'raw') { //If the raw visiblity is selected.. we set html to our raw data.
	    $nodeData['html'] = $_POST['sourceData'];
	    $output = $nodeData['raw'];
    }
    if($res = $bcms->getNodebyKey($_POST['key'])) {
  	  	$pof = $bcms->UpdateNode($nodeData);
  	  	debug("Updated node {$nodeData['key']}",0);
  	} else {
  	  	$pof = $bcms->addNode($nodeData);
  	  	debug("Added node {$nodeData['key']}",0);
  	}
  	//Now lets update our cache...
  	$res = $bcms->getNodebyKey($nodeData['key'],1);
    $cache_data = array('html' => $res['html'],'debug' => $afdebug);
    $cache->set($nodeData['key'],$cache_data,MEMCACHE_COMPRESSED,$caTimeout);
    
}
 
	//This function will check to see if our page link exists.. if so.. we link it!
} else {
 doHtml();
}
$qtime = sprintf("%.5f", microtime(true)-$time);
debug("Gen in $qtime",0);

function doHtml() {
global $bcms,$output;

if(!isset($_POST['action'])) { $action = 'blank'; } else { $action = $_POST['action']; }

if($action == 'Grab'){
	$res = $bcms->getNodebyKey($_POST['key']);
	$key = $_POST['key'];
	$raw = $res['raw'];
	$vis = $res['visible'];
} elseif($action == 'Preview' || $action == 'Commit') {
	$key = $_POST['key'];
	$raw = $_POST['sourceData'];
	$vis = $_POST['visible'];
} elseif(isset($_GET['req'])) {
	$res = $bcms->getNodebyKey($_GET['req']);
	$key = $_GET['req'];
	$raw = $res['raw'];
	$vis = $res['visible'];
} else {
	$vis = 'visible';
	$raw = '';	$key = $_REQUEST['req'];
}

$optionList = $bcms->getVisArray();
$optRndr = '<select name="visible">';
foreach ($optionList as $keyval => $value) {
	if(strstr($vis,$value)){
		$optRndr .= "<option value='$value' selected='selected'>$vis</option>";
	} else {
		$optRndr .= "<option value='$value'>$value</option>";
	}	
}

$rndnList = "<div class='list'>";

if(isset($_GET['offset'])) { $offset = $_GET['offset']; } else { $offset = 0; }
if(isset($_GET['limit'])) { $limit = $_GET['limit']; } else { $limit = 10; }


 $nodeList = $bcms->getNodeList($offset,$limit);
 foreach($nodeList as $tmpValue) {
	$rndnList .= "
<div class='listValue'>
<a href='?req={$tmpValue['key']}'>Edit</a> - {$tmpValue['key']} | {$tmpValue['timestamp']}
</div>";
 }
 

print <<<Page
<html>
<head>
<style type="text/css">
body {background-color: #fff;}
h1 {background-color: #00ff00}
h2 {background-color: transparent}
div#editor { background-color: #00E0E0; }
.prev { background-color: #00E0E0; }
#site { background-color: #000; color:#Fff; }
#lister { background-color: #01E5E5; color:#000; }
p {background-color: rgb(250,0,255)}
</style>
</head>
<body>
<div id="lister">
$rndnList
	<form method="GET">
		Offset: <input type="text" name="offset" value="$offset" size='4'/>
		Limit:	<input type="text" name="limit" value="$limit" size='4'/>
		<input type="hidden" name="req" value='$key''/>
		<input type="submit" name="list" value="Get List" />
	</form>
</div>
<div id='editor'">
	<form method="POST">
	Page Key <input type="text" name="key" value='$key''/> - <input type="submit" name="action" value="Grab" /><br />
	Visibility: 
		 $optRndr
		</select>
	<br />
	<textarea name="sourceData" rows="25" cols="80">$raw</textarea>
	<input type="submit" name="action" value="Preview" />
	<input type="submit" name="action" value="Commit" />
</form>
</div>
<div class='prev'>
<h2>Document Preview</h2>
</div>
<div id="site">
$output
</div>
</body>
</html>
Page;

}
function pageExists($page) {
global $bcms;
  if($bres = $bcms->getNodebyKey($page)) {
	  return true; //exists..
	} else {
		return false; //does not
	}
}
?>
